package com.itheima.dianming.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dianming.aop.HasAuth;
import com.itheima.dianming.pojo.domain.StuInterface;
import com.itheima.dianming.pojo.domain.StuProject;
import com.itheima.dianming.pojo.vo.R;
import com.itheima.dianming.service.StuInterfaceService;
import com.itheima.dianming.service.StuProjectService;
import com.itheima.dianming.service.StuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stuInterface")
@Slf4j
public class StuInterfaceController {

    @Autowired
    private StuInterfaceService stuInterfaceService;

    @Autowired
    private StuService stuService;


    @GetMapping
    public R queryPage(Integer page,Integer pageSize,Long projectId,String name){
        IPage<StuInterface> pageInfo = new Page<>(page,pageSize);
        stuInterfaceService.queryPage(pageInfo,name,projectId);
        Map map = new HashMap();
        map.put("records",pageInfo.getRecords());
        map.put("total",pageInfo.getTotal());
        return R.data(map);
    }

    @PostMapping
    @HasAuth("ROLE_ADMIN")
    public R save(@RequestBody StuInterface project){
        boolean save = stuInterfaceService.save(project);
        return R.onflag(save,save?"":"操作失败！");
    }

    @PutMapping
    @HasAuth("ROLE_ADMIN")
    public R update(@RequestBody StuInterface project){
        boolean save = stuInterfaceService.updateById(project);
        return R.onflag(save,save?"":"操作失败！");
    }

    @DeleteMapping("/{id}")
    @HasAuth("ROLE_ADMIN")
    public R deleteById(@PathVariable Integer id){
        boolean b = stuInterfaceService.removeById(id);
        return R.onflag(b);
    }

    @GetMapping("/list")
    public R list(){
        List<StuInterface> list = stuInterfaceService.list();
        return R.data(list);
    }
}
